package com.linjian.togetherbirthday.utils;


import android.util.Base64;

import com.yuntongxun.plugin.common.common.utils.Md5utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESOperator {

    /*
       * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式，key需要为16位。
       */
    private String sKey = "iyouwoquiyouwoqu";//key，可自行修改
    private String ivParameter = "iyouwoquiyouwoqu";//偏移量,可自行修改
    private static AESOperator instance = null;

    private AESOperator() {

    }

    public static AESOperator getInstance() {
        if (instance == null)
            instance = new AESOperator();
        return instance;
    }

    // 加密
    public String encrypt(String sSrc) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
//base64编码
//String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
        String strBase64 = Base64.encodeToString(encrypted, Base64.DEFAULT);
//base64解码
        String str2 = new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT));
        String aaa = Base64.encodeToString(encrypted, Base64.DEFAULT);
        aaa = aaa.replace("\n","");
        return aaa;// 此处使用BASE64做转码。
    }

    // 解密
    public String decrypt(String sSrc) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = Base64.decode(sSrc.getBytes(), Base64.DEFAULT);// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

//    public static void main(String[] args) throws Exception {
//
//  AESOperator aesOperator = new AESOperator();
////     Log.i("qqqqq",aesOperator.encryptTwo(new Date()));
////        System.out.println(aesOperator.encrypt("success"));
//        Log.i("qqqq",aesOperator.encrypt("success"));
//
//    }



    // 加密
    public String encryptTwo(Date date)  {

        String time = String.valueOf(date.getTime());
        String iviv = time.substring(time.length()-8);
        String iii = iviv+iviv;


        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = sKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(iii.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal("success".getBytes("utf-8"));
//base64编码
//String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
            String strBase64 = Base64.encodeToString(encrypted, Base64.DEFAULT);
//base64解码
            String str2 = new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT));
            String aaa = Base64.encodeToString(encrypted, Base64.DEFAULT);
            aaa = aaa.replace("\n","");
            return Md5utils.md5(aaa);// 此处使用BASE64做转码。

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
 